<template>
	<view>
		<!-- 提现 -->
		<view class="withdrawal-box">
			<!-- <view class="w-header">
				<view class="u-w-100 u-text-center" @click="close">
					<u-icon name="close"></u-icon>
				</view>
				<view class="u-flex-1"><text>提现</text></view>
				<view class="u-w-100 u-text-center">
					
				</view>
			</view> -->
			<view class="hd">
				<view @click="pay.show = true">
					<view class="u-flex">
						<view class="label"><text>到账至</text></view>
						<view class="u-flex-1">
							<view class="u-flex">
								<view class="u-flex-1">
									<u-icon v-if="way == 0" name="weixin-circle-fill" color="#2aae67" size="36"
										class="u-p-r-10"></u-icon>
									<u-icon v-if="way == 1" name="rmb-circle-fill" color="#ffaa00" size="36"
										class="u-p-r-10"></u-icon>
									<text class="u-font-32">{{pay.list[way].name}}</text>
								</view>
								<u-icon name="arrow-right" color="#909399"></u-icon>
							</view>
						</view>
					</view>
					<view class="tips"><text>单次提现{{websetting.withdrawalautoexamine}}元以上需人工审核，请知悉</text></view>
				</view>
				
				<u-form ref="uForm" label-width="150" class="u-p-t-30" v-if="way == 1">
					<u-form-item label="户名"><u-input v-model="bankname" placeholder="请输入银行户名" :border="true" /></u-form-item>
					<u-form-item label="银行账号"><u-input v-model="bankaccount" placeholder="请输入银行账号" :border="true" /></u-form-item>
					<u-form-item label="开户行"><u-input v-model="bankopenname" placeholder="请输入开户行" :border="true" /></u-form-item>
				</u-form>
			</view>
			<view class="bd">
				<view class="u-font-28"><text>提现金额</text></view>
				<view class="u-flex">
					<u-icon name="rmb" color="#000" size="70"></u-icon>
					<input class="input-num" type="digit" v-model="withdrawal" :disabled="allowEdit">
				</view>
				<view class="error u-p-t-30" v-if="error">
					<text v-if="withdrawal > websetting.withdrawalsinglelimit">单笔提现最高{{websetting.withdrawalsinglelimit}}元</text>
					<text v-else>输入金额超过余额</text>
				</view>
				<view class="u-font-28 u-tips-color u-p-t-30" v-else>
					<text>当前余额{{balance}},提现不能少于0.1元,</text>
					<text class="u-type-primary" @click="withdrawal = balance">全部提现</text>
				</view>
				<view class="bottom-box u-p-t-30">
					<u-button type="primary" class="wbutton" :disabled="disabled"
						@click="getWithdrawalApply">提现</u-button>
					<view class="u-font-28 u-tips-color u-p-t-30">
						<view class="u-p-b-20"><text>温馨提示：</text></view>
						<view class="u-p-b-4"><text>1. 请先进行微信实名验证再提现。未实名验证的微信账号，提现会出现未到账情况；</text></view>
						<view class="u-p-b-4"><text>2. 由于微信相关规定，单笔提现最高{{websetting.withdrawalsinglelimit}}元，一天提现最高{{websetting.withdrawaldailylimit}}元；</text></view>
						<view class="u-p-b-4"><text>3. 若出现未到账情况，请联系后台客服电话：<text class="u-type-primary" @click="call(websetting.telphone)">{{websetting.telphone}}</text></text></view>
					</view>
				</view>
			</view>
		</view>

		<u-picker @confirm="pickerPayment" v-model="pay.show" range-key="name" :default-selector="[0]" :range="pay.list"
			mode="selector"></u-picker>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		name:"withdrawal",
		props:{
			userid:{
				type: Number,
				default: 0
			},
			enterpriseid:{
				type: Number,
				default: 0
			},
			all:{
				type: Number,
				default: 0
			},
		},
		data() {
			return {
				withdrawal: null,
				money: 0,
				disabled: true,
				error: false,
				myWallet: 0,
				way: 0,
				bankname: '',
				bankaccount: '',
				bankopenname: '',
				balance: 0,
				// 支付方式
				pay: {
					show: false,
					list: [{
							value: 0,
							name: '微信钱包'
						},
						{
							value: 1,
							name: '线下打款'
						}
					]
				}
			};
		},
		computed: {
			...mapState({
				userdetail: state => state.userdetail,
				websetting: state => state.webSetting
			}),
			allowEdit(){
				if(this.all > 0){
					if(this.way == 1){
						this.error = false
						return false
					}else{
						return true
					}
				}else{
					return false
				}
			}
		},
		watch: {
			withdrawal(val) {
				// if(Number(val) > this.statistics.balance){ //超过账户金额不能点击提现按钮
				console.log(val)
				if ((Number(val) > this.websetting.withdrawalsinglelimit && this.way == 0) || Number(val) > this.balance) { //当到账方式是微信钱包时,超过后台设置的微信单次提现限额不能点击提现按钮
					this.disabled = true
					this.error = true
				} else {
					this.disabled = false
					this.error = false
				}
				
				if (Number(val) == 0) {
					this.disabled = true
				}
			}
		},
		onBackPress(e) {
			console.log('监听返回按钮事件', e);
			uni.showToast({
				title: e.from,
				icon: 'none'
			})
			return false;
		},
		mounted() {
			console.log('websetting',this.websetting)
			this.$store.dispatch('getDetails').then((res)=>{
				// this.$refs.withdrawal.getStatistics()
				console.log('my页面成功获取用户详情：',res)
				if(this.userid > 0){
					this.balance = res.balance
				}
				if(this.enterpriseid > 0){
					this.balance = res.bindinfo.enterpriselist.find((item)=>item.id == this.enterpriseid).balance
				}
				if(this.all > 0){
					this.withdrawal = res.balancetotal
					this.balance  = res.balancetotal
				}
			}).finally(()=>{
				// this.loading = false
				uni.hideLoading()
			})
			// this.getStatistics()
			// this.getMyWallet()
		},
		methods: {
			close(){
				this.$emit('close',false)
			},
			pickerPayment(e) {
				this.way = e[0]
				this.disabled = false
				// this.withdrawal = ''
			},
			// getWithdrawal() { //提现
			// 	this.bindWeixin().then(() => {
			// 		this.$u.get('/User/BillDetails/Withdrawal?amount=' + this.withdrawal).then((res) => {
			// 			uni.showToast({
			// 				title: res.msg
			// 			})
			// 		})
			// 	})
			// },
			getWithdrawalApply() { //发起提现申请
				console.log('发起提现申请',this.way)
				// if (this.way == 0) {
				// 	this.bindWeixin().then(() => {
				// 		this.withdrawalApply()
				// 	})
				// } else {
				// 	this.withdrawalApply()
				// }
				this.withdrawalApply()
			},
			withdrawalApply() { //提现申请方法
				let form = {
					item: [
					    {
							enterpriseId: this.enterpriseid || 0,
							amount: Number(this.withdrawal)
					    }
					],
					way: this.way
				}
				if(this.all > 0){
					form.item = []
					form.item.push({
						enterpriseId: 0,
						amount: this.userdetail.balance
					})
					this.userdetail.bindinfo.enterpriselist.forEach((item)=>{
						form.item.push({
							enterpriseId: item.id,
							amount: item.balance
						})
					})
					form.item = form.item.filter((item)=>item.amount > 0)
				}
				// 当到账方式选择的是银行账号时
				if (this.way == 1) {
					form.bankname = this.bankname,
						form.bankaccount = this.bankaccount,
						form.bankopenname = this.bankopenname
			
					if (form.bankname == '') {
						uni.showToast({
							title: '请填写银行户名',
							icon: 'none'
						})
						return
					}
					if (form.bankaccount == '') {
						uni.showToast({
							title: '请填写银行账号',
							icon: 'none'
						})
						return
					}
					if (form.bankopenname == '') {
						uni.showToast({
							title: '请填写开户行',
							icon: 'none'
						})
						return
					}
				}
			
				this.$u.post('/WithdrawApply/Create', form).then((res) => {
					uni.showToast({
						title: res.msg
					})
					uni.redirectTo({
						url: '/pages/my/wallet/success/success'
					})
				}).catch(()=>{
					this.bindWeixin()
				})
			},
			bindWeixin() {
				return new Promise((resolve, reject) => {
					if (this.userdetail.openid == '') {
						uni.showModal({
							title: '您尚未绑定微信',
							content: '需要绑定已实名微信才能成功提现,是否帮您绑定微信并申请提现？',
							confirmText: "确定绑定",
							success: (res)=> {
								if (res.confirm) {
									// #ifdef MP-WEIXIN
									uni.login({
										provider: 'weixin', //使用微信登录
										success: ret => {
											var code = ret.code
											this.$u.get('/User/Account/BindingWeiXin?code=' +code).then(data => {
												if (data.status == 0) {
													uni.showModal({
														content: '绑定成功，是否需要马上提现？',
														success: ()=>{
															this.withdrawalApply()
														}
													}),
													
													// uni.showToast({
													// 	title: data.msg
													// })
													resolve(res.msg)
												}
											})
										},
										fail: err => {
											reject()
										}
									})
									// #endif
								} else if (res.cancel) {
									reject()
								}
							},
							complete:function(res){
								console.log('complete',res)
							}
						})
					} else {
						resolve()
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	// 提现
	.withdrawal-box {
		background-color: #f4f4f5;
		height: 100vh;
		display: flex;
		flex-direction: column;

		.w-header {
			display: flex;
			align-items: center;
			text-align: center;
			height: 44px;
		}
	
		.hd {
			padding: 60rpx;
	
			.label {
				color: $u-tips-color;
				width: 150rpx;
			}
	
			.tips {
				font-size: 24rpx;
				color: $u-tips-color;
				padding-left: 150rpx;
				margin-top: 10rpx;
			}
		}
	
		.bd {
			display: flex;
			flex-direction: column;
			flex: 1;
			background-color: #fff;
			border-top-left-radius: 12rpx;
			border-top-right-radius: 12rpx;
			padding: 60rpx;
	
			.u-flex {
				padding-top: 60rpx;
				border-bottom: 1px solid $u-border-color;
	
				.input-num {
					flex: 1;
					font-size: 100rpx;
					height: 140rpx;
				}
			}
	
			.bottom-box {
				flex: 1;
	
				.wbutton {
					// padding: 60rpx;
				}
			}
	
			.error {
				color: $u-type-error;
			}
		}
	}
</style>